package org.example;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.e175.klaus.solarpositioning.AzimuthZenithAngle;
import net.e175.klaus.solarpositioning.DeltaT;
import net.e175.klaus.solarpositioning.SPA;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.time.*;
import java.util.TimeZone;

/**
 * ClassName: ${NAME}
 * Package: org.example
 * Description:
 *
 * @Author BuTang
 * @Create ${DATE} ${TIME}
 * @Version 1.0
 */
@Slf4j
public class SpaUtils {

    /**
     * 使用的库 solarpositioning-0.1.10.jar
     * @param localDateTime 需要计算太阳高度角的时间
     * @param zoneId 时区 举例：东八区为ZoneOffset.ofHours(8)
     * @param latitude 纬度（角度制）
     * @param longitude 经度（角度制）
     * @param elevation 海拔高度 （m）
     * @param pressure 年平均压力 单位为：millibars (or hectopascals) 若不确定，则填1000较为合理
     * @param temperature 年平均温度 单位 摄氏度
     * @return 返回结果中 getAzimuth 获取方位角（角度制）， getZenithAngle 获取天顶角 （角度制）
     *          注释：天顶角和高度叫互余（即天顶角+方位角=90°）
     *
     *  这里使用的是 https://dev.qweather.com/ 免费的api
     *  key为 95569e2830c54729b26a4734d61c5094
     */
    public static AzimuthZenithAngle getAzimuthAndZenithAngle(LocalDateTime localDateTime, ZoneId zoneId, double latitude, double longitude , double elevation , double pressure , double temperature) throws Exception {

            //创建http请求
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet("https://devapi.qweather.com/v7/weather/now?"+"location="+longitude+","+latitude+"&key=95569e2830c54729b26a4734d61c5094");
            CloseableHttpResponse response = httpClient.execute(httpGet);
            log.info("http请求为：{}",httpGet.toString());
            //获取状态码
            int statusCode = response.getStatusLine().getStatusCode();
            log.info("请求状态为：{}",statusCode);
            HttpEntity entity = response.getEntity();

            String entityString = EntityUtils.toString(entity);
            JSONObject data = JSONObject.parseObject(entityString);

            WeatherResponceEntity weatherResponceEntity = new WeatherResponceEntity();
            weatherResponceEntity = JSONArray.parseObject(data.toJSONString(),WeatherResponceEntity.class);
            String weatherData = data.getString("now");
            log.info("压力为：{}",weatherResponceEntity.now.pressure);
            log.info("温度为：{}",weatherResponceEntity.now.temp);



        log.info("开始计算太阳高度角与方位角");

        ZonedDateTime time = ZonedDateTime.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth(), localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond(), localDateTime.getNano(), zoneId);

        //计算deltaT
        double deltaT = DeltaT.estimate(LocalDate.from(localDateTime));
        //计算太阳位置
        AzimuthZenithAngle result = SPA.calculateSolarPosition(time, latitude, longitude, elevation, deltaT, weatherResponceEntity.now.pressure, weatherResponceEntity.now.temp);
//        AzimuthZenithAngle result = SPA.calculateSolarPosition(time, latitude, longitude, elevation, deltaT, pressure, temperature);

        //获取天顶角和方位角
        double azimuth = result.getAzimuth();
        double zenithAngle = result.getZenithAngle();
        log.info("时间的时间节点为：{}",time);
        log.info("高度角为：{}",(90-zenithAngle));
        log.info("方位角为：{}",azimuth);
        return result;
    }
    @Test
    public void test1() throws Exception {
        getAzimuthAndZenithAngle(LocalDateTime.now(), ZoneOffset.ofHours(8),39.92931,98.99834,1344,1000,20);
    }

    @Test
    public void test2(){
        int a = 1;
        int b = 1;

        assert  a > b;
    }


}